package com.pacy.swj.swjwb.activitys;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.gyf.immersionbar.ImmersionBar;
import com.pacy.swj.swjwb.R;
import com.pacy.swj.swjwb.Utils.Constans;
import com.pacy.swj.swjwb.Utils.DividerDecoration;
import com.pacy.swj.swjwb.Utils.HttpUtils;
import com.pacy.swj.swjwb.adapters.FinalListAdapter;
import com.pacy.swj.swjwb.adapters.SearchAdapter;
import com.pacy.swj.swjwb.beans.BaseBean;
import com.pacy.swj.swjwb.beans.QureyMapBean;
import com.pacy.swj.swjwb.beans.SearchBean;
import com.pacy.swj.swjwb.views.HRecyclerView;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import h.framework.android.utils.JsonUtil;
import h.framework.android.utils.LogUtil;
import h.framework.android.utils.ScreenUtil;
import h.framework.android.utils.StringUtil;
import h.framework.android.utils.ToastUtil;

public class SearchActivity extends AppCompatActivity {
    @BindView(R.id.title_et)
    EditText title_et;
    @BindView(R.id.search_tv)
    TextView search_tv;

    SearchAdapter mAdapter;
    LinearLayoutManager manager;
    @BindView(R.id.recyclerView)
    HRecyclerView recycler;
    String title;

    View notDataView;
    View errorView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ImmersionBar.with(this).statusBarColor(R.color.colorPrimary).init();
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        initRecyclerView();
    }


    /**
     * 初始化列表
     */
    private void initRecyclerView() {

        notDataView = getLayoutInflater().inflate(R.layout.empty2_view, null, false);
        errorView = getLayoutInflater().inflate(R.layout.error_view, null, false);

        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendRequest(title);
            }
        });

        errorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendRequest(title);
            }
        });

        mAdapter = new SearchAdapter();
        manager = new LinearLayoutManager(SearchActivity.this);
        DividerDecoration itemDecoration = new DividerDecoration(Color.WHITE, ScreenUtil.dp2px(2));
//        recycler.addItemDecoration(itemDecoration);
        recycler.setLayoutManager(manager);
        recycler.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ReadActivity.show(SearchActivity.this,"标题",Constans.DOWNLOAD_PATH+"test.xlsx");
                Intent intent = new Intent(SearchActivity.this, WebActivity.class);
                intent.putExtra("title", mAdapter.getItem(position).getTitle());
                intent.putExtra("id", mAdapter.getItem(position).getId());
                intent.putExtra("right", "首页");
                startActivity(intent);
            }
        });

    }


    /**
     * 请求具体内容
     */
    private void sendRequest(String title) {
        Map<String, String> map = new HashMap<>();
        map.put("title", title);
        HttpUtils.PostForm(Constans.SEARCH, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.i(result);
                BaseBean<SearchBean> baseBean = JsonUtil.jsonToObject(result, new TypeToken<BaseBean<SearchBean>>() {
                }.getType());

                if (baseBean.isSuccess()) {
                    mAdapter.setEmptyView(notDataView);
                    if (baseBean.getData() != null) {
                        if (baseBean.getData().getRecords().size() > 0) {
                            mAdapter.setNewData(baseBean.getData().getRecords());
                        }
                    }

                    List<SearchBean.RecordsBean> list = new ArrayList<>();
                    if (baseBean.getData() != null) {
                        if (baseBean.getData().getRecords().size() > 0) {
                            for (SearchBean.RecordsBean recordsBean :baseBean.getData().getRecords()) {
                                if (recordsBean.getStatus() .equals("1")) {
                                    list.add(recordsBean);
                                }
                            }
                        }
                    }
                    mAdapter.setEmptyView(notDataView);
                    mAdapter.setNewData(list);
                } else {
                    ToastUtil.error( baseBean.getMessage());
                    mAdapter.setEmptyView(errorView);
                    mAdapter.setNewData(null);
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtil.i(ex.toString());
                ToastUtil.error("请求出错，请重试");
                mAdapter.setEmptyView(errorView);
                mAdapter.setNewData(null);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @OnClick(value = {R.id.back_iv, R.id.search_tv})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_iv:
                finish();
                break;

            case R.id.search_tv:
                title=title_et.getText().toString();
                if (StringUtil.isEmpty(title)) {
                    ToastUtil.info("请输入搜索内容");
                } else {
                    sendRequest(title);
                }

                break;
        }
    }


}
